/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function dialogNewUser(newUserId) {
    $.ajax({
        "url":'a_register',
        "data":{"employeeId":newUserId},
        "type":'GET',
        "success":function(htmlData) {
            $.Dialog({
                "title": "Создать пользователя",
                "draggable": true,
                "content": htmlData,
                "width" : 600
            });
            disableBlockableItems(newUserId === -1);
        }
    });
}

function disableBlockableItems(state) {
    $(".register_blockable").each(function(){
        this.disabled = state; 
    });
}

function  reloadEmplCombo() {
    var officeId = document.getElementById("register_select_office").value;
    disableBlockableItems(true);
    var select = document.getElementById("register_select_employee")
    select.innerHTML = '<option value=\"-1\">Выберите сотрудника</option>\n';
    select.selectedIndex = 0;
    if (officeId !== 'NONE') {
        document.getElementById("register_select_office").options[0].disabled = true;
        $.ajax({
           "url":'a_get_employees_from',
           "data":{"officeId":officeId},
           "type":'GET',
           "dataType":'json',
           "success":function(data) {
               if (data.length != null) {
                    for(var i=0;i<data.length;i++) {
                        select.innerHTML += '<option value=\"' + data[i].id + '\">' + data[i].surnameNamePatronymic + '</option>\n';
                    }
               }
           }
        });
    }
}

function setEmployeeId() {
   var emplId = document.getElementById("register_select_employee").value;
   document.getElementById('register_employee_id').value = emplId;
   if (emplId !== '-1') disableBlockableItems(false);
   else disableBlockableItems(true);
}

function submitNewUser() {
    var string = $('#register_form').serialize();
    $.ajax({
            "url":'a_register',
            "type":'POST',
            "data": string,
            "success": function(data,textStatus) {
                    var err = $(data).find('#register_validation_error').val();
                    if (err === 'false') {
                        
                        dialogSuccessfulClose('Пользователь успешно зарегистрирован');
                                                                       
                        redrawDataTable('select_user_table');
                        
                    } else {
                        document.getElementById('register_main').innerHTML = data;
                    }
                }
             });
        }
    